{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import gym\n",
    "import numpy as np\n",
    "import sys\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[33mWARN: gym.spaces.Box autodetected dtype as <class 'numpy.float32'>. Please provide explicit dtype.\u001b[0m\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 42 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 45 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 25 timesteps\n",
      "Game over...\n",
      "Episode finished after 41 timesteps\n",
      "Game over...\n",
      "Episode finished after 32 timesteps\n",
      "Game over...\n",
      "Episode finished after 37 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 33 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 48 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 36 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 30 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 25 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 30 timesteps\n",
      "Game over...\n",
      "Episode finished after 28 timesteps\n",
      "Game over...\n",
      "Episode finished after 18 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 18 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 61 timesteps\n",
      "Game over...\n",
      "Episode finished after 43 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 33 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 44 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 45 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 49 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 38 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 18 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 29 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 25 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 30 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 32 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 41 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 10 timesteps\n",
      "Game over...\n",
      "Episode finished after 34 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 44 timesteps\n",
      "Game over...\n",
      "Episode finished after 33 timesteps\n",
      "Game over...\n",
      "Episode finished after 18 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 37 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 18 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 34 timesteps\n",
      "Game over...\n",
      "Episode finished after 86 timesteps\n",
      "Game over...\n",
      "Episode finished after 18 timesteps\n",
      "Game over...\n",
      "Episode finished after 34 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 27 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 10 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 38 timesteps\n",
      "Game over...\n",
      "Episode finished after 37 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 21 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 51 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 34 timesteps\n",
      "Game over...\n",
      "Episode finished after 29 timesteps\n",
      "Game over...\n",
      "Episode finished after 58 timesteps\n",
      "Game over...\n",
      "Episode finished after 34 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 29 timesteps\n",
      "Game over...\n",
      "Episode finished after 30 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 10 timesteps\n",
      "Game over...\n",
      "Episode finished after 28 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 26 timesteps\n",
      "Game over...\n",
      "Episode finished after 40 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 64 timesteps\n",
      "Game over...\n",
      "Episode finished after 45 timesteps\n",
      "Game over...\n",
      "Episode finished after 14 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 36 timesteps\n",
      "Game over...\n",
      "Episode finished after 36 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 33 timesteps\n",
      "Game over...\n",
      "Episode finished after 41 timesteps\n",
      "Game over...\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 24 timesteps\n",
      "Game over...\n",
      "Episode finished after 46 timesteps\n",
      "Game over...\n",
      "Episode finished after 51 timesteps\n",
      "Game over...\n",
      "Episode finished after 35 timesteps\n",
      "Game over...\n",
      "Episode finished after 30 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 12 timesteps\n",
      "Game over...\n",
      "Episode finished after 35 timesteps\n",
      "Game over...\n",
      "Episode finished after 35 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 23 timesteps\n",
      "Game over...\n",
      "Episode finished after 38 timesteps\n",
      "Game over...\n",
      "Episode finished after 20 timesteps\n",
      "Game over...\n",
      "Episode finished after 31 timesteps\n",
      "Game over...\n",
      "Episode finished after 26 timesteps\n",
      "Game over...\n",
      "Episode finished after 15 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 77 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n",
      "Episode finished after 10 timesteps\n",
      "Game over...\n",
      "Episode finished after 13 timesteps\n",
      "Game over...\n",
      "Episode finished after 22 timesteps\n",
      "Game over...\n",
      "Episode finished after 28 timesteps\n",
      "Game over...\n",
      "Episode finished after 16 timesteps\n",
      "Game over...\n",
      "Episode finished after 19 timesteps\n",
      "Game over...\n",
      "Episode finished after 10 timesteps\n",
      "Game over...\n",
      "Episode finished after 28 timesteps\n",
      "Game over...\n",
      "Episode finished after 11 timesteps\n",
      "Game over...\n",
      "Episode finished after 58 timesteps\n",
      "Game over...\n",
      "Episode finished after 17 timesteps\n",
      "Game over...\n"
     ]
    }
   ],
   "source": [
    "env = gym.make(\"CartPole-v0\")\n",
    "# env = gym.wrappers.Monitor(env, 'cartpole-experiment-1', force=True)\n",
    "\n",
    "sumlist = []\n",
    "for t in range(200):\n",
    "    state = env.reset()\n",
    "    i = 0\n",
    "    while(True):\n",
    "        i += 1\n",
    "        env.render()\n",
    "        action = env.action_space.sample()\n",
    "        nA = env.action_space.n\n",
    "        state, reward, done, _ = env.step(action)\n",
    "        # print(state, action, reward)\n",
    "\n",
    "        if done:\n",
    "            print(\"Episode finished after {} timesteps\".format(i+1))\n",
    "            break\n",
    "    \n",
    "    sumlist.append(i)\n",
    "    print(\"Game over...\")\n",
    "    \n",
    "# env.monitor.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "env.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CartPole game iter average time is: 22.745\n"
     ]
    }
   ],
   "source": [
    "iter_time = sum(sumlist)/len(sumlist)\n",
    "print(\"CartPole game iter average time is: {}\".format(iter_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
